<template>
	<view class="order_item_container bc_fff br_8 plr_12">
		<view class="height_44 fx aic jcb bob_1 boc_grey">
			<view class="fx fx_nw">
				{{data.createTime||''}}
				<text class=" ml_4" style="color:#FE5A1C;" v-if="data.deliveryWay==1">自提</text>
			</view>
			<view style="color:#FE5A1C">
				{{data.orderStatusStr||''}}
			</view>
		</view>
		<view class="pb_12 bob_1 boc_grey flow_h">
			<view class="fx pt_12 fx_nw"
				v-if="!data.orderInfoList||!data.orderInfoList.length||data.orderInfoList.length===1">
				<CustomImage width="160rpx" height="160rpx" radius="8rpx"
					:src="getStaticUrl((data.orderInfoList && data.orderInfoList[0])?data.orderInfoList[0].image:data.image)">
				</CustomImage>
				<view class="ml_12 fx_1 flow_h" v-if="data.orderInfoList && data.orderInfoList[0]">
					<view class="ml_12 fx_1 flow_h" v-if="userType==9">
						<view class="fe_2 height_34 lh_17">
							{{data.orderInfoList[0].productName||''}}
						</view>
						<view class="mt_4 fc_999 height_17 lh_17 fx fx_nw jcb">
							<text>{{productRuleStr(data.orderInfoList[0].productRule||'')}}</text>
							<text>×{{data.orderInfoList[0].productCount||data.orderInfoList[0].payNum||0}}</text>
						</view>
					</view>
					<template v-else>
						<view class="fe">
							{{data.orderInfoList[0].productName||''}}
						</view>
						<view class="mt_4 fc_999 height_17 lh_17">
							{{productRuleStr(data.orderInfoList[0].productRule)||''}}
						</view>
						<view class="mt_4 fc_999">
							×{{data.orderInfoList[0].payNum||0}}
						</view>
						<view class="fz_16 fb">
							¥{{productAmount(data.orderInfoList[0])||0}}
						</view>
					</template>
				</view>
				<view class="ml_12 fx_1 flow_h" v-else>
					<view class="ml_12 fx_1 flow_h" v-if="userType==9">
						<view class="fe_2 height_34 lh_17">
							{{data.productName||''}}
						</view>
						<view class="mt_4 fc_999 height_17 lh_17 fx fx_nw jcb">
							<text>{{productRuleStr(data.productRule||'')}}</text>
							<text>×{{data.productCount||data.payNum||0}}</text>
						</view>
					</view>
					<template v-else>
						<view class="fe">
							{{data.productName||''}}
						</view>
						<view class="mt_4 fc_999 height_17 lh_17">
							{{data.productRuleStr||''}}
						</view>
						<view class="mt_4 fc_999">
							×{{data.productCount||data.payNum||0}}
						</view>
						<view class="fz_16 fb">
							¥{{productAmount(data)||0}}
						</view>
					</template>
				</view>

			</view>
			<view class="fx pt_12 fx_nw flow_x" v-else-if="data.orderInfoList">
				<view class="img_wrap pr" v-for="item,index in data.orderInfoList" :key="index">
					<view class="badge">
						<u-badge numberType="limit" bgColor="#FE0000" max="99" :value="item.payNum"
							v-if="item.payNum"></u-badge>
					</view>
					<CustomImage width="160rpx" height="160rpx" radius="8rpx" :src="getStaticUrl(item.image)">
					</CustomImage>
				</view>
			</view>
			<view class="mt_16 fx fx_nw aic jcb" v-if="userType!=9">
				<view class="fc_primary">
					<text v-if="data.orderType>1">{{orderTypeName(data)}}</text>
					<text v-else-if="data.orderStatus==2&&data.auditPass==false">审核拒绝</text>
				</view>
				<view>
					共{{data.productCount||0}}件商品 总金额
					<text class="ml_8" style="color:#FE5A1C">￥{{data.payAmount||0}}</text>
				</view>
			</view>
		</view>
		<view class="ptb_8 fx fx_nw jcb grid_8">
			<view class="fx fx_sh aic fx_1 flow_h">
				<CustomImage width="60rpx" height="60rpx" radius="30rpx" :src="getStaticUrl(userInfo.avatar)">
				</CustomImage>
				<text class="ml_8 fx_1 fe">{{userInfo.name||''}}</text>
			</view>
			<view class="fx fx_w jce grid_8 flow_h">
				<button class="custom_btn default lh_28 plr_12 br_18 fz_14" @click="handleDetail">查看详情</button>
				<navigator :hover-class="none" :url="`/pages/person/pages/receive?id=${data.id}&type=${type}`"
					v-if="$permisList(data.orderStatus,'确认收款')">
					<button class="custom_btn primary lh_28 plr_12 br_18 fz_14">确认收款</button>
				</navigator>
				<button class="custom_btn primary lh_28 plr_12 br_18 fz_14" @click="handleUploadLog"
					v-if="$permisList(data.orderStatus,'上传物流单',isSingle(data)&&!data.logisticsImageUrl)&&((userType==2&&data.deliveryWay==1)||userType!=2)">上传物流单</button>
				<button class="custom_btn default lh_28 plr_12 br_18 fz_14" @click="$emit('cancel',data)"
					v-if="$permisList(data.orderStatus,'取消下单', isSingle(data))">取消下单</button>
				<button class="custom_btn default lh_28 plr_12 br_18 fz_14" @click="handleConfirmDetail"
					v-if="$permisList(data.orderStatus,'认购清单')">认购清单</button>
				<button class="custom_btn primary lh_28 plr_12 br_18 fz_14" @click="handleUploadPayment"
					v-if="$permisList(data.orderStatus,'上传支付凭证')">上传支付凭证</button>
				<button class="custom_btn primary lh_28 plr_12 br_18 fz_14" @click="handleUploadPayment"
					v-if="$permisList(data.orderStatus,'上传尾款凭证')">上传尾款凭证</button>
				<button class="custom_btn primary lh_28 plr_12 br_18 fz_14" @click="$emit('confirm',data)"
					v-if="$permisList(data.orderStatus,'确认下单')">确认下单</button>
				<button class="custom_btn primary lh_28 plr_12 br_18 fz_14" @click="$emit('confirm',data, 'goods')"
					v-if="$permisList(data.orderStatus,'确认收货', isSingle(data))">确认收货</button>
				<button class="custom_btn primary lh_28 plr_12 br_18 fz_14" @click="$emit('confirm',data, 'install')"
					v-if="$permisList(data.orderStatus,'确认安装', isSingle(data))">确认安装</button>
				<button class="custom_btn default lh_28 plr_12 br_18 fz_14" @click="$emit('delete',data)"
					v-if="$permisList(data.orderStatus,'删除记录')">删除记录</button>
				<!-- <button class="custom_btn default lh_28 plr_12 br_18 fz_14" @click="$emit('check',data)"
					v-if="data.logisticsImageUrl&&userType!=5">查看物流单</button> -->
				<button class="custom_btn primary lh_28 plr_12 br_18 fz_14" @click="$emit('audit',data)"
					v-if="$permisList(data.orderStatus,'立即审核')">立即审核</button>
			</view>
		</view>
	</view>
</template>

<script>
	import CustomImage from '@/components/Image'
	import api from '@/service/config/api.js'
	import {
		getStaticUrl
	} from '@/utils/index.js'
	import {
		mapState
	} from 'vuex'
	export default {
		components: {
			CustomImage,
		},
		props: {
			type: {
				type: String | Number,
				default: 2
			},
			data: {
				type: Object,
				default: () => ({})
			},
			orderData: {
				type: Object,
				default: () => ({})
			}
		},
		data() {
			return {

			};
		},
		computed: {
			...mapState(['userType', 'appType']),
			productRuleStr() {
				return data => {
					return data ? JSON.parse(data).map(item1 => item1.value).join(' ') : ''
				}
			},
			getStaticUrl() {
				return (src) => getStaticUrl(src)
			},
			productAmount() {
				return item => {
					if (this.data.orderType && this.data.orderType > 1 && this.data.orderType < 6) {
						return item.afterDiscountAmount || item.price || 0
					}
					if (this.goodsType && this.goodsType > 1 && this.goodsType < 6) {
						return item.afterDiscountAmount || item.price || 0
					}
					return this.appType === this.$base.USER_TYPE ? (item.productAmount || 0) : (item.purchasingPrice ||
						item.productAmount || 0)
				}
			},
			orderTypeName() {
				return item => {
					const obj = {
						2: '秒杀订单',
						3: '砍价订单',
						4: '抢购订单',
						5: '拼团订单',
						6: '积分订单',
					}
					return obj[item.orderType] || ''
				}
			},
			userInfo() {
				return this.data.userInfo ? JSON.parse(this.data.userInfo) : {}
			},
			// 是否单商品
			isSingle() {
				return dataItem => {
					if (dataItem.orderInfoList && dataItem.orderInfoList.length) {
						let count = dataItem.orderInfoList.length
						return count <= 1 ? true : dataItem.isSendTogether
					} else if (dataItem.orderBrandInfoList && dataItem.orderBrandInfoList.length) {
						let count = dataItem.orderBrandInfoList.reduce((num, item) => {
							return num + item.productInfoList.length
						}, 0)
						return count <= 1 ? true : dataItem.isSendTogether
					} else {
						return true
					}
				}
			}
		},
		methods: {
			handleDetail() {
				
				if (this.data.id) {
					uni.navigateTo({
						url: `/pages/person/pages/order_detail?id=${this.data.id}&type=${this.type}`
					})
					console.log(this.data.id)
					// uni.navigateTo({
					// 	url: '/pages/person/pages/evaluate?o_id=' + this.data.id
					// })
				}
			},
			// 上传支付凭证
			handleUploadPayment() {
				if (this.data.id) {
					let payAmount = null
					let certificateType = 1
					if (this.data.payType == 1) {
						payAmount = this.data.payAmount
					} else {
						if (this.data.orderStatus == 5) {
							payAmount = this.data.waitPayDeposit
						} else {
							payAmount = this.data.waitPayFinal
						}
					}
					if (this.payType == 1) {
						if (this.data.orderStatus == 5) {
							certificateType = 3
						} else {
							certificateType = 2
						}
					} else {
						if (this.data.orderStatus == 5)
							certificateType = 1
					}

					uni.navigateTo({
						url: `/pages/person/pages/upload?id=${this.data.id}&payType=${this.data.payType}&type=payment&certificateType=${certificateType}&payAmount=${payAmount||0}`
					})
				}
			},
			handleUploadLog() {
				console.log(this.data.id)
				if (this.data.id) {
					uni.navigateTo({
						url: `/pages/person/pages/upload?id=${this.data.id}&type=logistics`
					})
				}
			},
			handleEvaluate() {
				uni.navigateTo({
					url: '/pages/person/pages/evaluate?o_id=' + this.data.id
				})
			},
			handleConfirmDetail() {
				uni.navigateTo({
					url: '/pages/person/pages/subscribe?id=' + this.data.id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.img_wrap+.img_wrap {
		margin-left: 24rpx;
	}

	.badge {
		position: absolute;
		top: -12rpx;
		right: -12rpx;
		z-index: 2;
	}
</style>